package cl.uchile.sti.authorization.dao;

import cl.uchile.sti.authorization.model.Modulo;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Julio Villane
 * Date: 12-12-12
 * Time: 08:51 AM
 */
public class ModuloDAO {


    private static EntityManager entityManager = cambiarPorSpring.getEntityManager();

    private ModuloDAO(){ }

    public static List<Modulo> find(){
        Query q =  entityManager.createQuery("select p from Pagina p ");
        List <Modulo> resultado=q.getResultList();
        return resultado;
    }
    public static Modulo findID(Long id){
        return entityManager.find(Modulo.class, id);
    }
    public static Modulo findActive(Long active){
        return entityManager.find(Modulo.class, active);
    }
    public static Modulo findKey0(Long key){
        return entityManager.find(Modulo.class, key);
    }
    public static Modulo findNombre(Long nombre){
        return entityManager.find(Modulo.class, nombre);
    }
    public static Modulo findTipo(Long tipo){
        return entityManager.find(Modulo.class, tipo);
    }
    public static Modulo findURL(Long url){
        return entityManager.find(Modulo.class, url);
    }

    public static Modulo save(Modulo modulo) {
        if (modulo.getId() == null) {
            entityManager.persist(modulo);
            return modulo;
        } else {
            return entityManager.merge(modulo);
        }
    }

    public static void delete(Modulo modulo){
        entityManager.remove(modulo);
    }
}
